package practise;

import java.util.*;
class MyStack {

    public Queue<Integer> queue1;
    public Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (queue1.isEmpty() && queue2.isEmpty()) {
            queue1.offer(x);
        } else if (queue2.isEmpty()) {
            queue1.offer(x);
        } else {
            queue2.offer(x);
        }
    }

    public int pop() {
        int size = queue1.size() + queue2.size();
        if (!queue1.isEmpty() && queue2.isEmpty()) {
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        }else if (queue1.isEmpty() && !queue2.isEmpty()) {
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        }
        return -1;
    }

    public int top() {
        int size = queue1.size() + queue2.size();
        if (!queue1.isEmpty() && queue2.isEmpty()) {
            for (int i = 0; i < size - 1; i++) {
                queue2.offer(queue1.poll());
            }
            int m = queue1.peek();
            queue2.offer(queue1.poll());
            return m;
        }else if (queue1.isEmpty() && !queue2.isEmpty()) {
            for (int i = 0; i < size - 1; i++) {
                queue1.offer(queue2.poll());
            }
            int m = queue2.peek();
            queue1.offer(queue2.poll());
            return m;
        }
        return -1;
    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}